package FragmentUtils;




import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/20.
 */
public class FragmentUtils {
   static FragmentManager manager;
    private FragmentTransaction transaction;
    private Fragment fragment;
    private static List<Fragment> fragments;//存储
    private Fragment curmanager;

    public  List<Fragment> getFragments() {
        return fragments;
    }

    public static FragmentUtils getFragmentUtil(FragmentManager manager1){
        FragmentUtils fragmentUtils = new FragmentUtils();
        manager = manager1;
        fragments=new ArrayList<Fragment>();
        return fragmentUtils;
    }


    public   void addFragment(int id,Fragment fragment,String Tag){
        transaction = manager.beginTransaction();
        transaction.add(id,fragment,Tag);
       // transaction.addToBackStack(Tag);

        transaction.commit();
    }
    public  void reMoveFragment(String Tag){
        fragment = manager.findFragmentByTag(Tag);
        transaction = manager.beginTransaction();
        transaction.remove(fragment);
        transaction.commit();
    }

    public  void rePlaceFragment01(int id,Fragment fragment){
        transaction = manager.beginTransaction();
        transaction.replace(id,fragment);
        transaction.commit();
    }
    public  void rePlaceFragment(int id,String Tag){
        fragment = manager.findFragmentByTag(Tag);
        transaction = manager.beginTransaction();
        transaction.replace(id,fragment);
        transaction.addToBackStack(Tag);
        transaction.commit();
    }
    public  void show(String Tag){
        fragment = manager.findFragmentByTag(Tag);
        transaction = manager.beginTransaction();
        transaction.show(fragment);
        transaction.addToBackStack(Tag);
        transaction.commit();
    }
    public  void hide(String Tag){
        fragment = manager.findFragmentByTag(Tag);
        transaction = manager.beginTransaction();
        transaction.hide(fragment);
        transaction.addToBackStack(Tag);
        transaction.commit();
    }

    public void addContent(int id,Fragment from,Fragment to,String Tag){
            transaction = manager.beginTransaction();
        if(!to.isAdded()){
            transaction.add(id,to,Tag);
            fragments.add(to);
            transaction.addToBackStack(Tag);
            transaction.hide(from);
            transaction.commit();
        }else {
            transaction.hide(from);
            transaction.show(to);
            transaction.commit();
        }
    }

    /**
     * 得到当前的fragment
     * @return
     */
    public Fragment getCurmanager(List<Fragment> fragments){
        for(int i =0;i<fragments.size();i++){
            if(fragments.get(i).isVisible()){
                curmanager = fragments.get(i);
            }
        }

        return curmanager;
    }

}
